package com.miao.im.config;

import com.miao.im.common.Constant;
import com.miao.im.dispatcher.MessageDispatcher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/3/10 16:29
 * @Description:
 */
@Configuration
@ConditionalOnClass(RabbitTemplate.class)
@Slf4j
public class RabbitDispatcherConfiguration {

    @Bean("dispatchTopicExchange")
    public Exchange dispatchTopicExchange() {
        return ExchangeBuilder.topicExchange(Constant.DISPATCHER_EXCHANGE).durable(true).build();
    }

    @Bean("dispatchQueue")
    public Queue dispatchQueue() {
        return QueueBuilder.durable(Constant.DISPATCHER_QUEUE_NAME)
                .exclusive().autoDelete().build();
    }

    /**
    * 广播队列
    * @author miaoguoxin
    * @date 2021/3/16
    */
    @Bean("dispatchBroadcastQueue")
    public Queue dispatchBroadcastQueue() {
        return QueueBuilder.durable(Constant.DISPATCHER_BROADCAST_QUEUE_NAME)
                .autoDelete().build();
    }

    @Bean
    public Binding bindingTopicExchange() {
        return BindingBuilder.bind(dispatchQueue()).to(dispatchTopicExchange())
                .with(Constant.DISPATCHER_ROUTING_KEY_NAME).noargs();
    }

    /**
    * 绑定广播队列
    * @author miaoguoxin
    * @date 2021/3/16
    */
    @Bean
    public Binding bindingBroadcastTopicExchange() {
        return BindingBuilder.bind(dispatchBroadcastQueue()).to(dispatchTopicExchange())
                .with(Constant.DISPATCHER_BROADCAST_ROUTING_KEY_NAME).noargs();
    }

}
